package com.kingstar.ceres.data.strategy.csv;

import com.alibaba.fastjson.JSONObject;
import com.kingstar.ceres.data.bo.CsvDataConfig;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public abstract class DefaultCsvChannel implements ICsvChannel{

    @Resource
    ICsvChannelManager csvChannelManager;

    @Override
    public void process(List<List<String>> payload, CsvDataConfig config) {
        String[] headers = config.getHeaders().split(",");
        if (log.isDebugEnabled()){
            log.debug("准备发送下游sdk条数:{},数据:{}",payload.size(),payload);
        }
        List<JSONObject> list = payload.stream().map(value -> {
            JSONObject json = new JSONObject();
            int size = Math.min(headers.length, value.size());
            for (int i = 0; i < size; i++) {
                json.put(headers[i],value.get(i));
            }
            return json;
        }).collect(Collectors.toList());
        payload.clear();
        doProcess(config.getMatchTable(),list);
    }

    protected abstract void doProcess(String type, List<JSONObject> payload);

    protected void register(ICsvChannel channel){
        csvChannelManager.register(channel);
    }
}
